/*
 * Copyright (c) 2021 Huawei Device Co.,  Ltd.
 * Licensed under the Apache License,  Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,  software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,  either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "recptprinter_srv_client.h"
#include "string_ex.h"
#include "datetime_ex.h"
#include "ipc_skeleton.h"
#include "iservice_registry.h"
#include "if_system_ability_manager.h"
#include "system_ability_definition.h"
#include "recptprinter_errors.h"
#include "recptprinter_common.h"
#include "recptprinter_hilog.h"
#include "recptprinter_observer_stub.h"

namespace SwanLink {
namespace Finance {
RecptprinterSrvClient::RecptprinterSrvClient() {}
RecptprinterSrvClient::~RecptprinterSrvClient() {}

ErrCode RecptprinterSrvClient::Connect()
{
    std::lock_guard<std::mutex> lock(mutex_);
    if (proxy_ != nullptr) {
        return ERR_OK;
    }
    sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
    if (sysMgr == nullptr) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_SRV,  "failed to GetSystemAbilityManager, %{public}d", FINANCE_RECPTPRINTER_SERVICE_ID);
        return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
    }
    sptr<IRemoteObject> remoteObject_ = sysMgr->CheckSystemAbility(FINANCE_RECPTPRINTER_SERVICE_ID);
    if (remoteObject_ == nullptr) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_SRV,  "failed to GetSystemAbility by %{public}d", FINANCE_RECPTPRINTER_SERVICE_ID);
        return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
    }

    deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new RecptprinterSrvDeathRecipient());
    if (deathRecipient_ == nullptr) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_SRV,  "failed to Create DeathRecipinent, %{public}d", FINANCE_RECPTPRINTER_SERVICE_ID);
        return ERR_NO_MEMORY;
    }
    if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) {
        RECPTPRINTER_HILOGW(RECPTPRINTER_SRV,  "Add death recipient to failed, %{public}d", FINANCE_RECPTPRINTER_SERVICE_ID);
        return E_ADD_DEATH_RECIPIENT_FAILED;
    }

    proxy_ = iface_cast<IRecptprinterSrv>(remoteObject_);

    return ERR_OK;
}

void RecptprinterSrvClient::ResetProxy(const wptr<IRemoteObject>& remote)
{
    std::lock_guard<std::mutex> lock(mutex_);
    RETURN_IF(proxy_ == nullptr);
    auto serviceRemote = proxy_->AsObject();
    if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
        serviceRemote->RemoveDeathRecipient(deathRecipient_);
        proxy_ = nullptr;
    }
}

void RecptprinterSrvClient::RecptprinterSrvDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
{
    if (remote == nullptr) {
        return;
    }
    RecptprinterSrvClient::GetInstance().ResetProxy(remote);
}

int RecptprinterSrvClient::GetPrintFormat(int handle, PrintFormatType ftt)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_JS_NAPI,  "GetPrintFormat failed");
        return -1;
    }
    return proxy_->GetPrintFormat(handle, ftt);
}

void RecptprinterSrvClient::SetPrintFormat(int handle, PrintFormatType ftt, int value)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_JS_NAPI,  "SetPrintFormat failed");
        return ;
    }
    return proxy_->SetPrintFormat(handle, ftt, value);
}

void RecptprinterSrvClient::GetDeviceSpec(int handle, DeviceSpec &spec)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_JS_NAPI,  "GetDeviceSpec failed");
        return ;
    }
    return proxy_->GetDeviceSpec(handle, spec);
}

int RecptprinterSrvClient::FeedPaper(int handle, int line)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_JS_NAPI,  "FeedPaper failed");
        return -1;
    }
    return proxy_->FeedPaper(handle, line);
}

int RecptprinterSrvClient::CutPaper(int handle, int line, int mode)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_JS_NAPI,  "CutPaper failed");
        return -1;
    }
    return proxy_->CutPaper(handle, line, mode);
}

int RecptprinterSrvClient::AddText(int handle, int x, int y, const std::string& text)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_JS_NAPI,  "AddText failed");
        return -1;
    }
    return proxy_->AddText(handle, x, y, text);
}

int RecptprinterSrvClient::AddBitmap(int handle, int x, int y, const std::string& lpath)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_JS_NAPI,  "AddBitmap failed");
        return -1;
    }
    return proxy_->AddBitmap(handle, x, y, lpath);
}

int RecptprinterSrvClient::AddQRCode(int handle, int x, int y, int w, const std::string& qr)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_JS_NAPI,  "AddQRCode failed");
        return -1;
    }
    return proxy_->AddQRCode(handle, x, y, w, qr);
}
    
int RecptprinterSrvClient::AddBarCode(int handle, int x, int y, BarCodeSpec &spec, const std::string& bar)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_JS_NAPI,  "AddBarCode failed");
        return -1;
    }
    return proxy_->AddBarCode(handle, x, y, spec, bar);
}

void RecptprinterSrvClient::StartPrint(int handle, int timeout, PrintListener *listener)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_JS_NAPI,  "StartPrint failed");
        return;
    }
    //  注册监视
    return proxy_->StartPrint(handle, timeout, listener);
}
}  // namespace Finance
}  // namespace SwanLink
